<?php
/**
 * 检验用户注册
 */
namespace App\Http\Middleware;

use App\Services\ChatRoomService\ChatService;
use Closure;
use Illuminate\Support\Facades\Input;

/*
 * 服务器请求是的校验，用于验证是不是授权的服务器
 * */
class AuthChatServer
{

    public function handle($request, Closure $next)
    {

//
        // 判断请求头中是否包含ORIGIN字段
        if(isset($request->server()['REMOTE_ADDR'])){
            $remote_ip = $request->server()['REMOTE_ADDR'];
            $chatService = new ChatService();
            $client_platform =  $chatService->getPlatformByIp($remote_ip);

            $inputData = $request->input();
            $inputData = json_decode(base64_decode($inputData['data']),true);
            if(!empty($client_platform)){

                $inputEncryption = $inputData['encryption'] ;
                unset($inputData['encryption']);

                $encryption =  md5( json_encode( $inputData) . $client_platform->sign_key);

                if($inputEncryption != $encryption) {
                    echo 'error sign';
                }else{
                    //把客户ip地址添加到数据里
                    $inputData['$emote_ip'] = $remote_ip;
                    $request->exchange = $inputData;
                    return $next($request);
                }
            }

        }

    }
}

